<template>
    <SysDialog :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
        @onClose="onClose" @onConfirm="commit">
        <template v-slot:content>
            <el-form :model="addModel" ref="addFormRef" label-width="90px" :inline="false" size="default">
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="heTongBianHao" label="合同编号">
                            <el-input v-model="addModel.heTongBianHao"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="heTongMingCheng" label="合同名称">
                            <el-input v-model="addModel.heTongMingCheng"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="keHuMingCheng" label="客户名称">
                            <el-input v-model="addModel.keHuMingCheng"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="qianDingShiJian" label="签订时间">
                             <el-date-picker v-model="addModel.qianDingShiJian" placeholder="请选择签订时间"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="sheJiJinDu" label="设计进度">
                            <el-input v-model="addModel.sheJiJinDu"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="shengChanJinDu" label="生产进度">
                             <el-input v-model="addModel.shengChanJinDu"  ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="caiGouJinDu" label="采购进度">
                             <el-input v-model="addModel.caiGouJinDu"  ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="jiaoHuoQi" label="交货期">
                            <el-input v-model="addModel.jiaoHuoQi"  ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="zongJia" label="总价">
                            <el-input v-model="addModel.zongJia"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item prop="beiZhu" label="备注">
                    <textarea rows="5" cols="100" v-model="addModel.beiZhu"></textarea>
                </el-form-item>

            </el-form>

        </template>
    </SysDialog>
</template>

<script setup lang="ts">

import SysDialog from '@/components/SysDialog.vue'
import useDialog from '@/hooks/useDialog';
import { nextTick, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { addUserApi, editUserApi, getListApi } from '@/api/ht';
import { EditType, Title } from '@/type/BaseType';
import { HtModel } from '@/api/ht/HtModel';
const { dialog, onClose, onConfirm, onShow } = useDialog()    
const show = (type: string, row?: HtModel) => {
    
    type == EditType.ADD ? dialog.title = Title.ADD : dialog.title = Title.EDIT
    dialog.height = 500
    dialog.width = 1000
    dialog.visible = true

    if (row) {
        nextTick(() => {
            Object.assign(addModel, row)
            console.log('addModel:', addModel);
        })
    }

    addFormRef.value?.resetFields()

    addModel.type = type;
};

defineExpose({
    show
})

const addModel = reactive<HtModel>({
    type: '',
    id: '',
    heTongBianHao: '',
    heTongMingCheng: '',
    keHuMingCheng: '',
    qianDingShiJian: '',
    jiaoHuoQi: '',
    zongJia: '',
    beiZhu: '',
    sheJiJinDu: '',
    shengChanJinDu: '',
    caiGouJinDu: '',

})

const addFormRef = ref<FormInstance>()


const emists = defineEmits(['onFresh'])  


const commit = () => {
    addFormRef.value?.validate(async (valid) => {
        if (valid) {
            let res = null;
            if (addModel.type == EditType.ADD) {
                res = await addUserApi(addModel);
            } else {
                res = await editUserApi(addModel);
            }
            console.log(res)
            if (res && res.code == 200) {
                ElMessage.success(res.msg)
                emists('onFresh')
                onClose()
            }
        }
    })
}
</script>
<style scoped></style>